//
//  H_RegisterVC.m
//  Honey
//
//  Created by 贾亚涵 on 16/5/7.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_RegisterVC.h"

#define Register_Phone_Num 500
#define Register_Nick 505
#define Register_PassWord1 510
#define Register_PassWord2 515



@interface H_RegisterVC ()<UITextFieldDelegate>
{
    
    UILabel * _backLabel;
    
    
    UIView * _registerMainV;
    UIView * _registerV1;
    UIView * _registerV2;
    UIView * _registerV3;
    UIView * _registerV4;
    UILabel * _registerL1;
    UILabel * _registerL2;
    UILabel * _registerL3;
    UILabel * _registerL4;
    UITextField * _registerNickTF;
    UITextField * _registerPassWordTF1;
    UITextField * _registerPassWordTF2;
    UITextField * _registerPhoneNumTF;
    NSString * _RNickStr;
    NSString * _RPassWStr1;
    NSString * _RPassWStr2;
    NSString * _RPhoneNumStr;
    UIButton * _registerBtn;

}

@end

@implementation H_RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor whiteColor];
    [self NaviBarBtn];
    [self createRegisterView];
}

-(void)NaviBarBtn
{
    UIImage * leftBtnImage = [[UIImage imageNamed:@"tl_photo_slide_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftBtnImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBerBtnClicked:)];
    _backLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 60, 40)];
    _backLabel.text = @"返回";
    _backLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_backLabel];
}

-(void)leftBerBtnClicked:(id)sender
{
    [self registerPackKeyboard];
    //返回主界面
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
//初始化注册界面
-(void)createRegisterView
{
    
    _registerMainV = [[UIView alloc] initWithFrame:CGRectMake(0, 84, Screen_Width, 205)];
    _registerMainV.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_registerMainV];
    
    
    _registerL1 = [self addRegisterUILabelWithRect:CGRectMake(0, 1, 90, 50) Text:@"用  户  名 :"];
    [_registerMainV addSubview:_registerL1];
    _registerV1 = [self addUIViewWithRect:CGRectMake(CGRectGetMaxX(_registerL1.frame)+1, 1, Screen_Width-90, 50)];
    [_registerMainV addSubview:_registerV1];
    _registerNickTF = [self addRegisterUITFWithPlaceholder:@"请输入2~8位用户名"];
    _registerNickTF.tag = Register_Nick;
    [_registerV1 addSubview:_registerNickTF];
    
    _registerL2 = [self addRegisterUILabelWithRect:CGRectMake(0, CGRectGetMaxY(_registerL1.frame)+1, 90, 50) Text: @"密        码 :"];
    [_registerMainV addSubview:_registerL2];
    _registerV2 = [self addUIViewWithRect:CGRectMake(CGRectGetMaxX(_registerL2.frame)+1, CGRectGetMaxY(_registerL1.frame)+1, Screen_Width-90, 50)];
    [_registerMainV addSubview:_registerV2];
    _registerPassWordTF1 = [self addRegisterUITFWithPlaceholder:@"请输入密码"];
    //密码是否设为密文
    _registerPassWordTF1.secureTextEntry = YES;
    _registerPassWordTF1.tag = Register_PassWord1;
    [_registerV2 addSubview:_registerPassWordTF1];
    
    _registerL3 = [self addRegisterUILabelWithRect:CGRectMake(0, CGRectGetMaxY(_registerL2.frame)+1, 90, 50) Text:@"确认密码 :"];
    [_registerMainV addSubview:_registerL3];
    _registerV3 = [self addUIViewWithRect:CGRectMake(CGRectGetMaxX(_registerL3.frame)+1, CGRectGetMaxY(_registerL2.frame)+1, Screen_Width-90, 50)];
    [_registerMainV addSubview:_registerV3];
    _registerPassWordTF2 = [self addRegisterUITFWithPlaceholder:@"请再次输入密码"];
    //密码是否设为密文
    _registerPassWordTF2.secureTextEntry = YES;
    _registerPassWordTF2.tag = Register_PassWord2;
    [_registerV3 addSubview:_registerPassWordTF2];
    
    _registerL4 = [self addRegisterUILabelWithRect:CGRectMake(0, CGRectGetMaxY(_registerL3.frame)+1, 90, 50) Text:@"手  机  号 :"];
    [_registerMainV addSubview:_registerL4];
    _registerV4 = [self addUIViewWithRect:CGRectMake(CGRectGetMaxX(_registerL3.frame)+1, CGRectGetMaxY(_registerL3.frame)+1, Screen_Width-90, 50)];
    [_registerMainV addSubview:_registerV4];
    _registerPhoneNumTF = [self addRegisterUITFWithPlaceholder: @"请输入手机号"];
    //设置键盘类型
    _registerPhoneNumTF.keyboardType = UIKeyboardTypeNumberPad;
    _registerPhoneNumTF.tag = Register_Phone_Num;
    [_registerV4 addSubview:_registerPhoneNumTF];
    
    _registerBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, Screen_Height/2+30, Screen_Width-30, 40)];
    _registerBtn.layer.cornerRadius = 8.0f;
    [_registerBtn setTitle:@"注  册" forState:UIControlStateNormal];
    _registerBtn.backgroundColor = [UIColor colorWithRed:246/255.0 green:157/255.0 blue:182/255.0 alpha:1];
    [_registerBtn addTarget:self action:@selector(RegisterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerBtn];
    
}
#pragma mark- addUIlabel
-(UILabel *)addRegisterUILabelWithRect:(CGRect)rect Text:(NSString *)text
{
    UILabel * _RLlabel = [[UILabel alloc] initWithFrame:rect];
    _RLlabel.text = text;
    _RLlabel.textAlignment = NSTextAlignmentCenter;
    _RLlabel.textColor = [UIColor grayColor];
    _RLlabel.font = [UIFont systemFontOfSize:15];
    _RLlabel.backgroundColor = [UIColor whiteColor];
    return _RLlabel;
}
#pragma mark- addUITextFiled
-(UITextField *)addRegisterUITFWithPlaceholder:(NSString *)placeholder
{
    UITextField * _RLTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, Screen_Width-100, 50)];
    _RLTextFiled.backgroundColor = [UIColor whiteColor];
    //关闭首字母自动大写
    _RLTextFiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
    //添加清除小标
    _RLTextFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    _RLTextFiled.placeholder = placeholder;
    [_RLTextFiled addTarget:self action:@selector(RLTFChanged:) forControlEvents:UIControlEventEditingChanged];
    _RLTextFiled.delegate = self;
    return _RLTextFiled;
}

//对注册信息的限制
-(void)RLTFChanged:(UITextField *)sender
{
    switch (sender.tag)
    {
            //用户名的输入长度的限制
        case Register_Nick:
        {
            if (_registerNickTF.text.length <= 8)
            {
                _RNickStr = _registerNickTF.text;
            }else
            {
                _registerNickTF.text = _RNickStr;
            }
        }
            break;
        case Register_PassWord1:
        {
            NSCharacterSet * CS = [[NSCharacterSet characterSetWithCharactersInString:PassWord_Can_Input] invertedSet];
            NSString * str = [[_registerPassWordTF1.text componentsSeparatedByCharactersInSet:CS] componentsJoinedByString:@""];
            if (_registerPassWordTF1.text.length <= 16 && [str isEqualToString:_registerPassWordTF1.text])
            {
                _RPassWStr1 = _registerPassWordTF1.text;
                
            }else
            {
                _registerPassWordTF1.text = _RPassWStr1;
            }
        }
            break;
        case Register_PassWord2:
        {
            NSCharacterSet * CS = [[NSCharacterSet characterSetWithCharactersInString:PassWord_Can_Input] invertedSet];
            NSString * str = [[_registerPassWordTF2.text componentsSeparatedByCharactersInSet:CS] componentsJoinedByString:@""];
            if (_registerPassWordTF2.text.length <= 16 && [str isEqualToString:_registerPassWordTF2.text])
            {
                _RPassWStr2 = _registerPassWordTF2.text;
                
            }else
            {
                _registerPassWordTF2.text = _RPassWStr2;
            }
            
        }
            break;
        case Register_Phone_Num:
        {
            NSCharacterSet * CS = [[NSCharacterSet characterSetWithCharactersInString:PhoneCanInput] invertedSet];
            NSString * str = [[_registerPhoneNumTF.text componentsSeparatedByCharactersInSet:CS] componentsJoinedByString:@""];
            
            if (_registerPhoneNumTF.text.length <= 11 && [str isEqualToString:_registerPhoneNumTF.text])
            {
                _RPhoneNumStr = _registerPhoneNumTF.text;
                
            }else
            {
                _registerPhoneNumTF.text = _RPhoneNumStr;
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark- UIView
-(UIView *)addUIViewWithRect:(CGRect)rect
{
    UIView * mainV = [[UIView alloc] initWithFrame:rect];
    mainV.backgroundColor = [UIColor whiteColor];
    return mainV;
}

#pragma UITextFieldDelegate
//点击界面view 触发  收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_registerNickTF resignFirstResponder];
    [_registerPassWordTF1 resignFirstResponder];
    [_registerPassWordTF1 resignFirstResponder];
    [_registerPhoneNumTF resignFirstResponder];
}

#pragma mark- 收键盘
-(void)registerPackKeyboard
{
    [_registerNickTF resignFirstResponder];
    [_registerPassWordTF1 resignFirstResponder];
    [_registerPassWordTF2 resignFirstResponder];
    [_registerPhoneNumTF resignFirstResponder];
}


-(void)RegisterBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        //收起键盘
        [self registerPackKeyboard];
        //调用提示框第三方
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.color = [UIColor orangeColor];
        
        //判断用户名是否为空
        if (_registerNickTF.text.length != 0)
        {
            //判断用户名的长度
            if (_registerNickTF.text.length >= 2 && _registerNickTF.text.length <= 8)
            {
                //判断密码是否为空
                if (_registerPassWordTF1.text.length != 0)
                {
                    //判断密码的长度
                    if (_registerPassWordTF1.text.length >= 6 && _registerPassWordTF1.text.length <= 16)
                    {
                        //判断确认密码是否为空
                        if (_registerPassWordTF2.text.length != 0)
                        {
                            //密码校对
                            if ([_registerPassWordTF2.text isEqualToString: _registerPassWordTF1.text])
                            {
                                //判断手机号是否为空
                                if (_registerPhoneNumTF.text.length != 0)
                                {
                                    //手机号判断
                                    if (_registerPhoneNumTF.text.length == 11)
                                    {
                                        if ([@"13,15,17,18" containsString:[_registerPhoneNumTF.text substringToIndex:2]])
                                        {
                                            [hud hide:YES];
                                            [self checkInfoFromMYData];
                                            
                                        }else
                                        {
                                            //设置为文本模式
                                            [hud setMode:MBProgressHUDModeText];
                                            [hud setLabelText:@"请输入正确的手机号"];
                                            [hud hide:YES afterDelay:1];
                                        }
                                        
                                    }else
                                    {
                                        //设置为文本模式
                                        [hud setMode:MBProgressHUDModeText];
                                        [hud setLabelText:@"请输入11位手机号"];
                                        [hud hide:YES afterDelay:1];
                                    }
                                    
                                } else
                                {
                                    [hud setMode:MBProgressHUDModeText];
                                    [hud setLabelText:@"手机号为空，请输入手机号"];
                                    [hud hide:YES afterDelay:1];
                                }
                                
                            }else
                            {
                                //设置为文本模式
                                [hud setMode:MBProgressHUDModeText];
                                [hud setLabelText:@"两次密码输入不同"];
                                [hud hide:YES afterDelay:1];
                            }
                        }else
                        {
                            [hud setMode:MBProgressHUDModeText];
                            [hud setLabelText:@"请输入确认密码"];
                            [hud hide:YES afterDelay:1];
                        }
                    }else
                    {
                        //设置为文本模式
                        [hud setMode:MBProgressHUDModeText];
                        [hud setLabelText:@"请输入6~16位密码"];
                        [hud hide:YES afterDelay:1];
                    }
                    
                }else
                {
                    [hud setMode:MBProgressHUDModeText];
                    [hud setLabelText:@"密码为空，请输入密码"];
                    [hud hide:YES afterDelay:1];
                }
            } else
            {
                //设置为文本模式
                [hud setMode:MBProgressHUDModeText];
                [hud setLabelText:@"请输入2~8位的昵称"];
                [hud hide:YES afterDelay:1];
            }
            
        }else
        {
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"用户名为空，请输入用户名"];
            [hud hide:YES afterDelay:1];
        }
        
        
        
        
    }
}

//数据库校对
-(void)checkInfoFromMYData
{
    H_CoreDataDAL * dal1 = [H_CoreDataDAL new];
    //调用提示框第三方
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor orangeColor];
    
    if (![dal1 checkRepeatedlyPhoneNumber:_registerPhoneNumTF.text])
    {
        if (![dal1 checkRepeatedlyNick:_registerNickTF.text])
        {
            [dal1 saveRegisterInfo:_registerPhoneNumTF.text :_registerNickTF.text :_registerPassWordTF2.text];
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"您已经注册成功哟，请返回登录"];
            [hud hide:YES afterDelay:1];
            
        }else
        {
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"用户名重复，请重新输入"];
            [hud hide:YES afterDelay:1];
        }
        
    }else
    {
        [hud setMode:MBProgressHUDModeText];
        [hud setLabelText:@"手机号已注册，请重新输入"];
        [hud hide:YES afterDelay:1];
    }
}



@end
